package com.egao.common.module.system.controller;

import com.egao.common.core.annotation.OperLog;
import com.egao.common.core.exception.ParameterException;
import com.egao.common.core.web.BaseController;
import com.egao.common.core.web.JsonResult;
import com.egao.common.core.web.PageParam;
import com.egao.common.core.web.PageResult;
import com.egao.common.module.system.entity.Major;
import com.egao.common.module.system.service.MajorService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 专业管理
 * @author pyx
 */
@Api(tags = "学院专业管理")
@RestController
@RequestMapping("${api}")
public class MajorController extends BaseController {
    @Autowired
    private MajorService majorService;

    @PreAuthorize("hasAuthority('sys:major:list')")
    @ApiOperation("分页查询专业")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "xl", value = "学历", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "keyword", value = "编码或名称", required = false, dataType = "string", paramType = "query"),
    })
    @GetMapping("/sys/major/list")
    public PageResult<Major> tree(HttpServletRequest request) {
        PageParam<Major> pageParam = new PageParam<>(request);
        pageParam.setDefaultOrder(null, new String[]{"create_time"});
        return new PageResult<>(majorService.listPage(pageParam), pageParam.getTotal());
    }


    @PreAuthorize("hasAuthority('sys:major:insert')")
    @OperLog(value = "专业管理", desc = "添加专业", result = true)
    @ApiOperation("添加专业")
    @PostMapping("/sys/major/insert")
    public JsonResult insert(@RequestBody Major major) {
        return majorService.insertMajor(major);
    }

    @PreAuthorize("hasAuthority('sys:major:update')")
    @OperLog(value = "专业管理", desc = "修改专业", result = true)
    @ApiOperation("添加专业")
    @PutMapping("/sys/major/update")
    public JsonResult update(@RequestBody Major major) {
        return majorService.updateMajor(major);
    }

    @PreAuthorize("hasAuthority('sys:major:delete')")
    @OperLog(value = "专业管理", desc = "删除专业", result = true)
    @ApiOperation("删除专业")
    @DeleteMapping("/sys/major/delete")
    public JsonResult delete(Integer id) {
        if(id == null){
            throw new ParameterException("参数对象为空");
        }

        if(majorService.removeById(id)){
            return JsonResult.ok("删除成功");
        }

        return JsonResult.error("删除失败");
    }
}
